package com.zyd.util;

import com.alibaba.fastjson.JSON;
import com.zyd.constants.SessionKeyConstants;
import com.zyd.io.vo.user.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

public class ToolUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(ToolUtils.class);

    public static Integer getInteger(Object obj) {
        if (!ToolUtils.isEmpty(obj)) {
            try {
                return Integer.parseInt(obj.toString().trim());
            } catch (Exception e) {
                return new Integer(0);
            }
        } else {
            return new Integer(0);
        }
    }

    public static boolean isEmpty(Object value) {
        if (value == null) {
            return true;
        }
        if ((value instanceof String)
                && (((String) value).trim().length() <= 0)) {
            return true;
        }
        if ((value instanceof Object[]) && (((Object[]) value).length <= 0)) {
            return true;
        }
        // 判断数组中的值是否全部为空null.
        if (value instanceof Object[]) {
            Object[] t = (Object[]) value;
            for (int i = 0; i < t.length; i++) {
                if (t[i] != null) {
                    return false;
                }
            }
            return true;
        }
        if ((value instanceof Collection) && ((Collection) value).size() <= 0) {
            return true;
        }
        if ((value instanceof Dictionary) && ((Dictionary) value).size() <= 0) {
            return true;
        }
        if ((value instanceof Map) && ((Map) value).size() <= 0) {
            return true;
        }
        return false;
    }

    public static String getNowTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
        String nowtime = df.format(new Date());
        return nowtime;
    }

    public static boolean isCoolectionEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }


    /**
     * 获取当前登录用户id
     *
     * @return
     */
    public static String getUserId(HttpServletRequest request) {
        return getUser(request).getId();
    }

    public static UserVO getUser(HttpServletRequest request) {
        String info = (String) request.getSession().getAttribute(SessionKeyConstants.LOGIN_USER);
        Map<String, Object> response = JSON.parseObject(info, Map.class);
        LOGGER.info("session信息:" + JSON.toJSONString(response));

        //登录用户不存在
        if (response == null) {
            return new UserVO();
        }
        return (UserVO) response.get("user");
    }

    /**
     * 加密用户密码
     *
     * @param passwd
     * @return
     */
    public static String encodePasswd(String passwd) {
        String encodePwd = MD5Utils.MD5Encode(passwd + MD5Utils.PRIVAEKEY,
                "UTF-8").toUpperCase();
        return encodePwd;
    }

    /**
     * 查询当前时间之前的几个小时的时间
     *
     * @param hour
     * @return
     */
    public static String getBeforeTime(int hour) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        c.setTime(new Date());
        c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) - hour);
        return df.format(c.getTime());
    }

    public static String getBeforeTime(int datenum, int hour) {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.MONTH, -datenum);  //设置为前3月
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour);
        dBefore = calendar.getTime();   //得到前3月的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore);    //格式化前3月的时间
        return defaultStartDate;
    }

    /**
     * 将一个String数组变成带括号字符,便于带in条件的SQL语句使用 例:('111','222','333','')
     *
     * @param array
     * @return
     */
    public static String combinArrayToString(String[] array) {
        if (isEmpty(array)) {
            return null;
        }

        StringBuffer str = new StringBuffer("('");
        for (int i = 0; i < array.length; i++) {
            if (ToolUtils.isEmpty(array[i])) {
                continue;
            }
            str.append(array[i]);
            if (array.length - 1 != i) {
                str.append("','");
            }

        }
        str.append("')");
        return str.toString();
    }

    public static String combinListToString(List<String> array) {
        if (isEmpty(array)) {
            return null;
        }

        StringBuffer str = new StringBuffer("('");
        for (int i = 0; i < array.size(); i++) {
            if (ToolUtils.isEmpty(array.get(i))) {
                continue;
            }
            str.append(array.get(i));
            if (array.size() - 1 != i) {
                str.append("','");
            }

        }
        str.append("')");
        return str.toString();
    }
}
